import com.fasterxml.jackson.databind.ObjectMapper;
import com.mysql.fabric.jdbc.FabricMySQLDataSource;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import javax.xml.transform.Result;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * user:likai
 * Date:2022-12-24
 * Time:15:42
 */
@WebServlet("/message")
public class MessageServlet extends HttpServlet {
    private ObjectMapper objectMapper = new ObjectMapper();

    //private List<Message> messageList=new LinkedList<>();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //显示的声明数据格式，不要让客户端猜
        resp.setContentType("applacation/json;charset=utf8");
        //resp.getWriter().write(objectMapper.writeValueAsString(messageList));
        try {
            resp.getWriter().write(objectMapper.writeValueAsString(load()));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Message message = objectMapper.readValue(req.getInputStream(), Message.class);
        //messageList.add(message);
        try {
            save(message);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        resp.setStatus(200);
        System.out.println("数据提交成功：from=" + message.getFrom() + ",to=" + message.getTo() +
                ",message=" + message.getMessage());
    }

    private List<Message> load() throws SQLException {
       DataSource dataSource=DBUtil.getDataSource();
        Connection connection=dataSource.getConnection();
        //构造sql语句
        String sql="select * from message";
        PreparedStatement statement=connection.prepareStatement(sql);

        ResultSet resultSet=statement.executeQuery();
        List<Message> result=new ArrayList<>();
        while(resultSet.next()){
            Message message=new Message();
            message.setFrom(resultSet.getString("from"));
            message.setTo(resultSet.getString("to"));
            message.setMessage(resultSet.getString("message"));
            result.add(message);
        }
        //关闭连接
        statement.close();
        connection.close();
        return result;
    }

    private void save(Message message) throws SQLException {
        DataSource dataSource=DBUtil.getDataSource();
        Connection connection=dataSource.getConnection();
        //构造sql语句
        String sql="insert into message values(?,?,?)";
        PreparedStatement statement=connection.prepareStatement(sql);
        statement.setString(1,message.getFrom());
        statement.setString(2,message.getTo());
        statement.setString(3,message.getMessage());

        //执行了几行
        int ret=statement.executeUpdate();
        System.out.println("ret="+ret);

        //关闭连接
        statement.close();
        connection.close();
    }
}